//
//  ScreenRulerScrollView.m
//  ScreenRuler
//
//  Created by Tianzhou on 08-12-23.
//  Copyright 2008 Tianzhou Software Inc.. All rights reserved.
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations under
//  the License.

#import "ScreenRulerScrollView.h"
#import "ScreenRulerExtensibleRulerView.h"
#import "ScreenRulerUnitTileView.h"
#import "ScreenRulerTransparentView.h"


@implementation ScreenRulerScrollView

+ (void)registerDefaults
{
	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
	
	NSDictionary *appDefaults = [NSDictionary dictionaryWithObjectsAndKeys:
								 [NSNumber numberWithInt:kLeft],
								 kScreenRulerVerticalRulerLocationKey,
								 [NSNumber numberWithInt:kTop],
								 kScreenRulerHorizontalRulerLocationKey,
								 nil];
	
	[defaults registerDefaults:appDefaults];
}

#if USE_SPECIALCURSOR_FOR_DRAGGING
- (void)mouseMoveReceived:(NSEvent *)theEvent
{
	NSPoint thePoint = [theEvent locationInWindow];
		
	if ([theEvent window] == nil)
	{
		thePoint = [[self window] convertScreenToBase:thePoint];
	}
	if ([self superview])
	{
		thePoint = [[self superview] convertPoint:thePoint fromView:nil];
	}
	
	NSView *theHitView = [self hitTest:thePoint];
	
	if (theHitView == [self horizontalRulerView])
	{
		[(ScreenRulerExtensibleRulerView *)[self horizontalRulerView] mouseMoveReceived:theEvent];
	}
	else if(theHitView == [self verticalRulerView])
	{
		[(ScreenRulerExtensibleRulerView *)[self verticalRulerView] mouseMoveReceived:theEvent];
	}
}
#endif

- (BOOL)isFlipped
{
	NSUserDefaultsController *defaults = [NSUserDefaultsController sharedUserDefaultsController];
	id values = [defaults values];
	NSNumber *horzRulerLocation = [values valueForKey:kScreenRulerHorizontalRulerLocationKey];
	if ([horzRulerLocation intValue] == kTop)
	{
		return YES;
	}
	else
	{
		return NO;
	}
}

- (void)awakeFromNib
{
	[ScreenRulerScrollView registerDefaults];
}

- (id)initWithFrame:(NSRect)frameRect
{
	if( (self = [super initWithFrame:frameRect]) != nil )
	{
		ScreenRulerExtensibleRulerView *theVertRuler = [[ScreenRulerExtensibleRulerView alloc] init];
		[theVertRuler setMeasurementUnits:@"Points"];
		[theVertRuler setReservedThicknessForMarkers:1.0];
		[theVertRuler setReservedThicknessForAccessoryView:0.0];
		[self setVerticalRulerView:theVertRuler];
		[theVertRuler setEnclosingScrollerView:self];
		[theVertRuler release];
		
		ScreenRulerExtensibleRulerView *theHorzRuler = [[ScreenRulerExtensibleRulerView alloc] init];
		[theHorzRuler setMeasurementUnits:@"Points"];
		[theHorzRuler setReservedThicknessForMarkers:1.0];
		[theHorzRuler setReservedThicknessForAccessoryView:0.0];
		[self setHorizontalRulerView:theHorzRuler];
		[theHorzRuler setEnclosingScrollerView:self];
		[theHorzRuler release];
		
		[self setRulersVisible:YES];
		
		ScreenRulerTransparentView *theDocumentView = [[ScreenRulerTransparentView alloc] initWithFrame:frameRect];
		// Override the [NSView autoResizingMask] doesn't work
		[theDocumentView setAutoresizingMask:NSViewMinXMargin|NSViewMaxXMargin|NSViewMinYMargin|NSViewMaxYMargin|NSViewWidthSizable|NSViewHeightSizable];
		[self setDocumentView:theDocumentView];
		[theDocumentView release];
		
		NSRect unitTileFrame = NSMakeRect(0, 0, [[self verticalRulerView] ruleThickness], [[self horizontalRulerView] ruleThickness]);
		ScreenRulerUnitTileView *theUnitTileView = [[ScreenRulerUnitTileView  alloc] initWithFrame:unitTileFrame
																			   enclosingScrollView:self];
		[self addSubview:theUnitTileView];
		[theUnitTileView release];
	}
	return self;
}

@end
